import React, { useEffect,memo,useState } from 'react'
import SearchBar from '@/components/searchBar/SearchBar'
import ClassifySecondmenu from '@/components/classify/classifySecondmenu/ClassifySecondmenu'
import ClassifyMenu from '@/components/classify/classifyMenu/ClassifyMenu'
import * as actionTypes from './store/actionCreators'
import {connect} from 'react-redux'
import {SearchFixed} from './style'
import { renderRoutes } from 'react-router-config'
import {HashRouter, Route,Switch,Link} from 'react-router-dom'

function Classify(props) {

    const {classifyList,classifyRecommand,classifySecondMenu,route} = props
    const {setClassifyList,getClassifyData}=props
    const [checkedIndex,setcheckedIndex] = useState(0)
    const [secondMenu,setSecondMenu] = useState([])
    // console.log(classifyList)
    useEffect(()=>{
        if(checkedIndex){
            setSecondMenu(classifySecondMenu[checkedIndex]?.data)
        }
    },[checkedIndex])

    useEffect(()=>{
        setClassifyList()
        getClassifyData()
    },[])

    return (
        <div>
            {
                renderRoutes(route.routes)
            }
            
            <SearchFixed>
                <SearchBar />
            </SearchFixed>
            <ClassifyMenu classifyList={classifyList} checkedIndex={checkedIndex} setcheckedIndex={setcheckedIndex} />
            <ClassifySecondmenu secondMenu={secondMenu} checkedIndex={checkedIndex} classifyRecommand={classifyRecommand} />
            
        </div>
    )
}

const mapStateToPorps=(state)=>{
    return {
        classifyList:state.classify.classifyList,
        classifyRecommand:state.classify.classifyRecommand,
        classifySecondMenu:state.classify.classifySecondMenu
    }
}

const mapStateToDispatch=(dispatch)=>{
    return {
        setClassifyList(){
            dispatch(actionTypes.getClassifyList())
        },
        getClassifyData(){
            dispatch(actionTypes.getClassifyData())
        }
    }
}

export default connect(mapStateToPorps,mapStateToDispatch)(memo(Classify))
